/*
 * Copyright (c) 2018. J4dream created
 */

package com.j4dream.property.javabasic.lock;

public class Ticket implements Runnable {
    private static int num = 100;
    Object obj = new Object();
    boolean flag = true;

    public void run() {
        if (flag) {
            while (num > 0) {
                synchronized (obj) {
                    show();
                }
            }
        } else
            while (num > 0) {
                show();
            }
    }

    public synchronized void show() {
        synchronized (obj) {
            if (num > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()
                        + "...function..." + num--);
            }
        }
    }
}
